using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.UI.WebControls;

namespace ProCodes.BugTracker.BugCatcher.Emails
{
    public partial class EmailAttachments : PRO_DOCS.BLL.WIN.Templates.DevxPermissionTemplate
    {
        private static List<string> attachedFiles;
        public static List<string> AttachedFiles
        {
            get
            {
                attachedFiles = (attachedFiles == null) ? new List<string>() : attachedFiles;
                return attachedFiles;
            }
            set { attachedFiles = value; }
        }

        private static int attachmentsSize;
        public static int AttachmentsSize
        {
            get { return attachmentsSize; }
            set { attachmentsSize = value; }
        }

        public EmailAttachments()
        {
            InitializeComponent();
        }

        private void EmailAttachments_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < AttachedFiles.Count; i++)
            {
                FileInfo fileInfo = new FileInfo(AttachedFiles[i]);
                ListViewItem listViewItem = new ListViewItem();
                int key = lstviewAttachedFiles.Items.Count + 1;
                listViewItem.Text = fileInfo.Name;
                listViewItem.Name = key.ToString();
                listViewItem.Tag = AttachedFiles[i];
                lstviewAttachedFiles.Items.Add(listViewItem);

                fileInfo = null;
            }
        }

        private void chkChooseAllItems_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < lstviewAttachedFiles.Items.Count; i++)
            {
                lstviewAttachedFiles.Items[i].Checked = chkChooseAllItems.Checked;
            }

        }

        private void btnAddAttachments_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Multiselect = true;
            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.Title = "Add Mail Attachments";
            if (openFileDialog.ShowDialog().Equals(DialogResult.OK))
            {
                long filesSize = 0;
                for (int i = 0; i < openFileDialog.FileNames.Length; i++)
                {
                    FileInfo fileInfo = new FileInfo(openFileDialog.FileNames[i]);
                    filesSize += fileInfo.Length;
                }
                if (filesSize <= 1048576 && (filesSize + AttachmentsSize) <= 1048576)
                {
                    AttachedFiles.AddRange(openFileDialog.FileNames);
                    for (int i = 0; i < openFileDialog.FileNames.Length; i++)
                    {
                        string filePath = openFileDialog.FileNames[i];
                        FileInfo fileInfo = new FileInfo(openFileDialog.FileNames[i]);
                        int key = lstviewAttachedFiles.Items.Count + 1;
                        ListViewItem listViewItem = new ListViewItem();
                        listViewItem.Text = fileInfo.Name;
                        listViewItem.Name = key.ToString();
                        listViewItem.Tag = filePath;
                        lstviewAttachedFiles.Items.Add(listViewItem);

                    }
                }
                else
                {
                    MessageBox.Show(Properties.Settings.Default.AttachmentsNotAccepted,
                                    String.Empty,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

            }
        }

        private void btnRemoveAttachments_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(PRO_DOCS.BLL.WIN.Properties.Messages.Default.ConfirmDeleteAttachedFilesText,
                    PRO_DOCS.BLL.WIN.Properties.Messages.Default.DeleteCaption,
                    MessageBoxButtons.OKCancel,
                    MessageBoxIcon.Asterisk,
                    MessageBoxDefaultButton.Button2).Equals(DialogResult.OK))
            {

                while (lstviewAttachedFiles.CheckedItems.Count > 0)
                {
                    lstviewAttachedFiles.Items.Remove(lstviewAttachedFiles.CheckedItems[0]);
                }
            }
            else
            {
                //No action
            }
        }

        private void btnReturnToMail_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }



        private void lstviewAttachedFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string filePath = lstviewAttachedFiles.SelectedItems[0].Tag.ToString();
            try
            {
                System.Diagnostics.Process.Start(filePath);
            }
            catch (Exception)
            {
                MessageBox.Show(Properties.Settings.Default.FileTypeNotAssociatedText.Replace("{nl}", Environment.NewLine),
                                Properties.Settings.Default.FileTypeNotAssociatedCaption,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }
    }
}